﻿using System;
using System.Xml.Linq;
using System.Collections.Generic;
using ThirdServices.AlertModel.Factory.BaseInterface;
using ThirdServices.TemperatureSettings;
using ThirdServices.AlertModel.Factory.Entities.DTO;
using ThirdServices.AlertModel.Analyzers;

namespace ThirdServices.AlertModel.Wanderground
{
    /// <summary>
    /// Contains information about temperature alerts on Wanderground service
    /// </summary>
    public class WandergroundTempAlert : IAlert
    {
        private double lat;
        private double lon;
        private double radius;
        private int numDataPerDay;

        private double percentage;
        private int periodInDays;

        public double Percentage
        {
            get { return percentage; }
            set { percentage = value; }
        }

        public int PeriodInDays
        {
            get { return periodInDays; }
            set { periodInDays = value; }
        }

        public double Lat
        {
            get { return lat; }
            set { lat = value; }
        }

        public double Lon
        {
            get { return lon; }
            set { lon = value; }
        }

        public int NumDataPerDay
        {
            get { return numDataPerDay; }
            set { numDataPerDay = value; }
        }

        #region IAlert Members

        /// <summary>
        /// Checkes for any temperature alertes
        /// </summary>
        /// <returns></returns>
        public List<BaseAlert> CheckAlert()
        {
            List<TemperatureData> TempData = new List<TemperatureData>();
            Console.WriteLine("WunderTemperatureAlert : recieving information...");
            string url = AlertsLinks.WanderTempAlertLink(lat, lon);
            XDocument xDoc = XDocument.Load(url);
            foreach (XElement xe in xDoc.Descendants("forecastday"))
            {
                DateTime dt = new DateTime(
                    Convert.ToInt32(xe.Element("date").Element("year").Value),
                    Convert.ToInt32(xe.Element("date").Element("month").Value),
                    Convert.ToInt32(xe.Element("date").Element("day").Value),
                    Convert.ToInt32(xe.Element("date").Element("hour").Value),
                    Convert.ToInt32(xe.Element("date").Element("min").Value),
                    Convert.ToInt32(xe.Element("date").Element("sec").Value)
                );
                TempData.Add(new TemperatureData
                {
                    Date = dt,
                    Temperature = (Convert.ToDouble(xe.Element("high").Element("celsius").Value) + Convert.ToDouble(xe.Element("low").Element("celsius").Value)) / 2
                });
            }
            List<BaseAlert> baseAlerts = TempAnalyser.AnalyseTemperatureData(TempData, percentage, periodInDays,numDataPerDay);
            return baseAlerts;
        }
        
        #endregion

        public void GetAlert()
        {
        }
    }
}